var app = getApp()
Page({
  data: {
    consigneeName: '',
    contactNumber: '',
    provinces: [],
    citys: [],
    countys: [],
    areaId: 0,
    areaName: ['', '', ''],
    areaValue: [0, 0, 0],
    detailAddress: '',
    selectedarea: '',
    sex: '',
    isDefault: 0,
    addressId: 0,//0表示新增
    arraySex: [{ 'sexname': '男' }, { 'sexname': '女' }],
    showpicker: true,
    author: 'Lotus'
  },
  closewin: function () {
    var that = this;
    var areaName = that.data.areaName;
    var selectedarea = '';
    for (var i in areaName) {
      selectedarea = selectedarea + ' ' + areaName[i];
    }
    that.setData({ selectedarea: selectedarea, showpicker: true });
  },
  cancelwin: function () {
    this.setData({ showpicker: true })
  },
  openwin: function () {
    this.setData({ showpicker: false })
  },
  changeSex: function (e) {
    this.setData({ sex: e.detail.value })
  },



  changeDefault: function (e) {
    if (e.detail.value) {
      console.log("set 1 :" + e.detail.value);
      this.setData({ isDefault: 1 });
    } else {
      this.setData({ isDefault: 0 });
      console.log("set 0 :" + e.detail.value);
    }

  },
  changeArea: function (e) {
    var that = this;
    //console.log("selected=" + e.detail.value);
    //console.log("areaValue=" + that.data.areaValue);
    var pid = e.detail.value;
    var provinces = that.data.provinces;
    var citys = that.data.citys;
    var countys = that.data.countys;
    var areaName = that.data.areaName;
    var areaId = that.data.areaId;
    var changearea = [0, 0, 0];
    areaName[0] = provinces[pid[0]].name;

    if (pid[0] != that.data.areaValue[0]) {
      var areaValue = [pid[0], 0, 0];
      pid = [pid[0], 0, 0];
      that.setData({ areaValue: areaValue });
      console.log("areaValue=" + that.data.areaValue);
      areaId = provinces[0].id;
      if (provinces[pid[0]].citys) {
        citys = provinces[pid[0]].citys;
        areaName[1] = citys[0].name;
        areaId = citys[0].id;
      }
      if (citys[0].area) {
        countys = citys[0].area;
        areaName[2] = countys[0].name;
        areaId = countys[0].id;
      }
    } else {
      console.log("province have not change");
      if (pid[1] != that.data.areaValue[1]) {
        console.log("city change=" + pid[1]);
        var areaValue = that.data.areaValue;
        areaValue[1] = pid[1];
        that.setData({ areaValue: areaValue });
        console.log("areaValue=" + that.data.areaValue);
        console.log("citys=" + citys);
        areaName[1] = citys[pid[1]].name;
        countys = citys[pid[1]].area;
        areaName[2] = countys[0].name;
        areaId = countys[0].id;

      } else {
        if (pid[2] != that.data.areaValue[2]) {
          var areaValue = that.data.areaValue;
          areaValue[2] = pid[2];
          that.setData({ areaValue: areaValue });
          console.log("areaValue=" + that.data.areaValue);
          areaName[2] = countys[pid[2]].name;
          areaId = countys[pid[2]].id;
        }
      }
    }

    //console.log("areaId=" + areaId);
    that.setData({
      citys: citys,
      countys: countys,
      areaName: areaName,
      areaId: areaId
    })
  },

  formSubmit: function (e) {
    var that = this;
    //var keyword = e.detail.value;  
    //console.log('form发生了submit事件，携带数据为：',e.detail.value);
    const params = e.detail.value
    console.log(params);
    //进行整理数据
    var nameaddress = e.detail.value.consigneeName;
    var mobileaddress = e.detail.value.contactNumber;
    var addressinfo = e.detail.value.detailAddr;
    var is_default = that.data.isDefault;
    var openidinfo = app.globalData.openId;
    //将获取的数据进行提交到后台
    app.util.request({
      method: 'POST',
      url: 'entry/wxapp/addaddress',
      data: { openid: openidinfo, name: nameaddress, mobile: mobileaddress, address: addressinfo, is_default: is_default },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        wx.redirectTo({
          url: '../index/addresslist',
        })
      },
      fail: function (res) {
        //console.log("fail:" + res);
      },
    })



  },
  formReset: function () {
    console.log('form发生了reset事件')
  },
  getarealist: function () {
    var that = this;
    /* 地区列表 */

    var service = 'ReceiptAddress.getAreaList';
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log("success:" + res.data);
        if (!res.data.data.code) {
          var provinces = res.data.data.list;
          that.setData({ provinces: provinces });
          that.getInfoByid();
        } else {
          console.log("fail:" + res.data);
        }
      },
      fail: function (res) {
        console.log("fail:" + res.data);
      }
    })

    /* 地区列表 */
  },
  getInfoByid: function () {
    var that = this;
    var addressId = that.data.addressId;
    /* 地址详细 */
    if (addressId) {
      var service = 'ReceiptAddress.getInfoByid';
      wx.request({
        method: 'POST',
        url: app.globalData.APIURL,
        data: { service: service, openId: app.globalData.openId, addressId: addressId },
        header: { 'Content-Type': 'application/x-www-form-urlencoded' },
        success: function (res) {

          if (!res.data.data.code) {
            var info = res.data.data.info;

            var consigneeName = info.consigneeName;
            var contactNumber = info.contactNumber;
            var isDefault = info.isDefault;
            var detailAddress = info.detailAddress;
            var areaId = info.areaId;
            var sex = info.sex;
            var areaName = [];
            areaName[0] = info.province;
            areaName[1] = info.city;
            areaName[2] = info.county;

            var areaValue = [];
            var getareaid = [];
            getareaid[0] = info.areaId.substr(0, 2) + "0000";
            getareaid[1] = info.areaId.substr(0, 4) + "00";
            getareaid[2] = info.areaId.substr(0, 6);

            var provinces = that.data.provinces;
            var provinceid;
            var thisid;

            for (var i in provinces) {
              if (provinces[i].id == getareaid[0]) {
                var citys = provinces[i].citys;
                areaValue[0] = parseInt(i);
              }
            }
            for (var i in citys) {
              if (citys[i].id == getareaid[1]) {
                var countys = citys[i].area;
                areaValue[1] = parseInt(i);
              }
            }
            for (var i in countys) {
              if (countys[i].id == getareaid[2]) {
                areaValue[2] = parseInt(i);
              }
            }

            that.setData({ consigneeName: consigneeName, contactNumber: contactNumber, isDefault: isDefault, sex: sex, detailAddress: detailAddress, areaId: areaId, areaName: areaName, citys: citys, countys: countys });
            that.setData({ areaValue: areaValue });


          } else {
            console.log("fail:" + res.data);
          }
        },
        fail: function (res) {
          console.log("fail:" + res.data);
        }
      })
    }
    /* 地址详细 */
  },
  onLoad: function (options) {
    var that = this;
    if (options.fromInviteCode) {
      app.globalData.fromInviteCode = options.fromInviteCode;
    }
    app.appstart();
    if (options.addressid) {
      this.setData({ addressId: options.addressid });
    }
    that.getarealist();

    that.WxValidate = app.WxValidate({
      consigneeName: {
        required: true,
        minlength: 2,
        maxlength: 10,
      },
      contactNumber: {
        required: true,
        minlength: 7,
      },
      detailAddr: {
        required: true,
        minlength: 5,
        maxlength: 60,
      },
    }, {
        name: {
          required: '请输入姓名',
        },
        tel: {
          required: '请输入电话',
        },
        address: {
          required: '请输入地址',
        },
      })

  }
})